﻿﻿using System.Collections.Generic;
using System.IO;
 using System.Text;
 using Newtonsoft.Json;
using UnityEngine;

namespace Tools
{
    public class FileTools<T>
    {
        private static readonly StringBuilder DataPathHelper = new StringBuilder();
        
        public static List<T> ReadJsonData(string fileName)
        {
            
            DataPathHelper.Append(Application.dataPath).Append("/").Append(fileName);
            // if (!File.Exists(RankPath))
            //     File.Create(RankPath);
            List<T> ranks = new List<T>();
            
            string json = File.ReadAllText(DataPathHelper.ToString());

            if (json != "")
            {
                ranks = JsonConvert.DeserializeObject<List<T>>(json); 
            }

            DataPathHelper.Clear();
            return ranks;
        }
        
        public static void WriteRankJson(List<T> ranks, string fileName)
        {
            DataPathHelper.Append(Application.dataPath).Append("/").Append(fileName);
            
            string json = JsonConvert.SerializeObject(ranks);
            File.WriteAllText(DataPathHelper.ToString(), json);
        
            DataPathHelper.Clear();
        }
        
    }
}